package net.chasing.androidbaseconfig.util;

import android.media.AudioRouting;
import android.media.MediaRecorder;
import android.os.Build;

import net.chasing.androidbaseconfig.util.thread.ThreadPoolUtils;

import java.io.IOException;

public class MediaRecorderUtil {

    private boolean isRecording = false;
    private String mFilePath;
    private MediaRecorder recorder;

    private Type type = Type.AAC_AAC;
    private long mStartTime;

    /**
     * 枚举了两种常用的类型
     */
    public enum Type {
//        AAC_M4A(".m4a", MediaRecorder.AudioEncoder.AAC, MediaRecorder.OutputFormat.MPEG_4),   // 经测试，录音后文件异常
        AAC_AAC(".aac", MediaRecorder.AudioEncoder.AAC, MediaRecorder.OutputFormat.AAC_ADTS),
        AMR_AMR(".amr", MediaRecorder.AudioEncoder.AMR_NB, MediaRecorder.OutputFormat.AMR_NB);
        String ext;
        int audioEncoder;
        int outputFormat;

        Type(String ext, int audioEncoder, int outputFormat) {
            this.ext = ext;
            this.audioEncoder = audioEncoder;
            this.outputFormat = outputFormat;
        }
    }

    public MediaRecorderUtil() {
    }

    /**
     * 开始录制
     */
    public String start(String rootPath) {
        if (isRecording) {
            return mFilePath;
        }
        isRecording = true;
        mFilePath = rootPath + "recorder_" + System.currentTimeMillis() + type.ext;
        try {
            recorder = new MediaRecorder();
            recorder.setOutputFile(mFilePath);
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(type.outputFormat);
            recorder.setAudioEncoder(type.audioEncoder);
            recorder.prepare();
            recorder.start();
            mStartTime = System.currentTimeMillis();
            L.i("MediaRecorderUtil start " + mStartTime);
            return mFilePath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean resume() {
        if (recorder != null)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                recorder.resume();
                return true;
            }
        return false;
    }

    public boolean pause() {
        if (recorder != null)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                recorder.pause();
                return true;
            }
        return false;
    }

    /**
     * 停止录制
     */
    public void stop() {
        if (!isRecording) {
            return;
        }
        long curTime = System.currentTimeMillis();
        if (curTime - mStartTime < 1000) {
            ThreadPoolUtils.getInstance().executeOnMainThread(this::stop, (int) (1000 - (curTime - mStartTime)));
        } else {
            if (recorder != null) {
                L.i("MediaRecorderUtil stop " + curTime);
                try {
                    recorder.stop();
                } catch (Exception e) {
                    L.i("MediaRecorderUtil stop error " + e.getMessage());
                }
                recorder.release();
                recorder = null;
            }
            isRecording = false;
            mFilePath = null;
        }
    }
}
